﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Business;
using System.Web.Security;

namespace BitPortal.Admin.Config.BaseConfig.Module
{
    public partial class ConfigurationAddEdit : System.Web.UI.UserControl
    {
        public Ext.Net.Window WindowContainer
        {
            get { return this.Window1; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [DirectMethod]
        public void ChangeEdit(string value)
        {
            RichEditor1.DataType = value;
        }

        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            try
            {
                RichEditor1.DataType = cbType.SelectedItem.Value;
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                PortalConfig config = bitEnt.PortalConfigs.SingleOrDefault(ent => ent.Name == txtKey.Text);
                if (config != null)
                {
                    config.DataType = RichEditor1.DataType;
                    config.Value = RichEditor1.Value;
                    config.AllowVisiable = ckAllowVisiable.Checked;
                    config.AllowEdit = ckAllowEdit.Checked;
                    config.AllowDelete = ckAllowDelete.Checked;
                    config.Description = txtDescription.Text;
                    config.Group = ConfigGroup1.ComboboxGroup.Text;
                    config.IsGlobalConfig = ckGlobalConfig.Checked;
                    if (bitEnt.SaveChanges() > 0)
                    {
                        Notification.Show(new NotificationConfig
                        {
                            Title = "Title",
                            Icon = Icon.Information,
                            Html = "Sửa cấu hình thành công"
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                X.Msg.Alert("Có lỗi xảy ra", ex.Message).Show();
            }

        }
        protected void btnInsert_Click(object sender, DirectEventArgs e)
        {
            try
            {
                RichEditor1.DataType = cbType.SelectedItem.Value;
                PortalConfig config = new PortalConfig()
                {
                    Name = txtKey.Text.Trim(),
                    DataType = RichEditor1.DataType,
                    Value = RichEditor1.Value,
                    AllowVisiable = ckAllowVisiable.Checked,
                    AllowDelete = ckAllowDelete.Checked,
                    AllowEdit = ckAllowEdit.Checked,
                    Description = txtDescription.Text,
                    Group = ConfigGroup1.ComboboxGroup.Text,
                    IsGlobalConfig = ckGlobalConfig.Checked
                };
                if (config.Insert() > 0)
                {
                    Notification.Show(new NotificationConfig
                    {
                        Title = "Title",
                        Icon = Icon.Information,
                        Html = "Thêm cấu hình thành công"
                    });
                    Window1.Hide();
                }
            }
            catch (Exception ex)
            {
                X.Msg.Alert("Có lỗi xảy ra", ex.Message).Show();
            }

        }

        public void BindData(string configName)
        {
            if (configName != null)
            {
                PortalConfig config = PortalConfigBLL.GetByName(configName);
                if (config != null)
                {
                    ckAllowDelete.Checked = config.AllowDelete;
                    ckAllowEdit.Checked = config.AllowEdit;
                    ckAllowVisiable.Checked = config.AllowVisiable;
                    txtKey.Text = config.Name;
                    txtKey.Enabled = false;
                    txtDescription.Text = config.Description;
                    RichEditor1.DataType = cbType.SelectedItem.Value = config.DataType;
                    RichEditor1.Value = config.Value;
                    ConfigGroup1.ComboboxGroup.Text = config.Group;
                    ckGlobalConfig.Checked = config.IsGlobalConfig;

                    btnUpdate.Hidden = false;
                    btnInsert.Hidden = true;
                    Window1.Show();
                }
            }
            else
            {
                RichEditor1.DataType = "String";
                cbType.SetValueAndFireSelect("String");
                txtDescription.Text = txtID.Text = txtKey.Text = RichEditor1.Value = "";
                btnUpdate.Hidden = true;
                btnInsert.Hidden = false;

                Window1.Show();
            }
        }
    }
}